package com.chinatelecom.ifree.business.dao.order.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.springframework.stereotype.Repository;

import com.chinatelecom.ifree.business.dao.order.FcodeDao;
import com.chinatelecom.ifree.business.domain.order.FcodeEntity;
import com.chinatelecom.ifree.business.domain.order.FcodeStatus;

@Repository
public class FcodeDaoImpl extends BaseDaoSupport<FcodeEntity> implements FcodeDao {

	@Override
	public void saveFcode(FcodeEntity entity) throws Exception {
		sql = "INSERT INTO fcode_info(F_CODE,F_STATUS,CREATE_TIME,UPDATE_TIME) VALUES(?,?,?,?)";
		super.saveEntity(entity);
	}

	@Override
	public void saveFcode(FcodeEntity[] entitys) throws Exception {
		sql = "INSERT INTO fcode_info(F_CODE,F_STATUS,CREATE_TIME,UPDATE_TIME) VALUES(?,?,?,?)";
		super.saveBatchEntity(entitys);
	}

	@Override
	public void updateFcodeState(String fcode, FcodeStatus status) throws Exception {
		sql = "UPDATE fcode_info SET F_STATUS=? WHERE F_CODE=?";
		super.updateEntity(fcode, status.ordinal());
	}

	@Override
	public FcodeEntity executeSingleResultSet(ResultSet rs) throws Exception {
		FcodeEntity entity = null;
		if (rs.next()) {
			entity = new FcodeEntity();
			entity.setFcode(rs.getString("F_CODE"));
			entity.setStatus(FcodeStatus.getStatus(rs.getInt("F_STATUS")));
			entity.setCreateTimes(rs.getTimestamp("CREATE_TIME"));
			entity.setUpdateTimes(rs.getTimestamp("UPDATE_TIME"));
		}
		return entity;
	}

	@Override
	public void executeSql(PreparedStatement state, FcodeEntity entity) throws Exception {
		state.setString(1, entity.getFcode());
		state.setInt(2, entity.getStatus().ordinal());
		state.setTimestamp(3, entity.getCreateTimes());
		state.setTimestamp(4, entity.getUpdateTimes());
	}

	@Override
	public void executeBatchSql(PreparedStatement state, FcodeEntity[] entitys) throws Exception {
		for (FcodeEntity entity : entitys) {
			state.setString(1, entity.getFcode());
			state.setInt(2, entity.getStatus().ordinal());
			state.setTimestamp(3, entity.getCreateTimes());
			state.setTimestamp(4, entity.getUpdateTimes());
			state.addBatch();
		}

	}

	@Override
	public FcodeEntity getFcodeEntity(String fcode) throws Exception {
		sql = "SELECT * FROM fcode_info WHERE F_CODE=?";
		return super.getEntityByPK(fcode);
	}
}
